<template>
	<view class="site">
		<u-cell-item title="新增资源" @click="handleNewSite">
			<text>默认资源</text>
		</u-cell-item>

		<u-swipe-action :index="index" v-for="(item, index) in list" :key="index" :options="options"
			@click="handleActionClick" @open="handleItemOpen">
			<u-cell-item :title="item.name" :arrow="false">
				<view class="u-m-r-10" v-if="site.index  == index">首选</view>
				<view slot="right-icon">
					<u-radio-group v-model="site.index" @change="switchChange">
						<u-radio :name="index" size="40"></u-radio>
					</u-radio-group>
				</view>
			</u-cell-item>
		</u-swipe-action>
		<u-toast ref="uToast" />
		<NewCku ref="refNewCku" @submit="handleSubmit"></NewCku>
	</view>
</template>

<script>
	import db from "@/utils/database.js"
	import http from "@/utils/request.js"
	import NewCku from "./components/newCku.vue"
	export default {
		components: { NewCku },
		data() {
			return {
				list: [],
				site: {
					name: '',
					index: -1
				},
				options: [{
						text: '编辑',
						style: {
							backgroundColor: '#00aa00'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			}
		},
		onLoad() {
			this.fetchData();
		},
		methods: {
			fetchData() {
				let list = [...this.cku]
				this.list = list.sort((a, b) => a.orderNum - b.orderNum)
				let index = list.findIndex(t => t.id == this.defcku.id)
				this.site.index = index
			},
			switchChange(index, e) {
				let site = this.list[index]
				this.$u.vuex('defcku', site)
				this.$refs.uToast.show({ title: '设置为首选项成功', type: 'success', duration: '2300' })
				// console.log(this.defcku)
			},
			handleItemOpen(index) {
				this.list[index].show = true
			},
			async handleActionClick(rowIndex, actionIndex) {
				let site = this.list[rowIndex]
				if (actionIndex == 0) {
					//编辑
					this.$set(site, 'show', false)
					this.$refs.refNewCku.handleModify(site, rowIndex)
					return
				}
				if (actionIndex == 1) {
					//删除 
					let index = this.cku.findIndex(t => t.id == site.id)
					if (index == -1) {
						//错误，数据不存在
						this.$u.toast('错误，数据不存在！')
					} else {
						if (this.cku[index].id == this.defcku.id) {
							//删除了默认首选项 
							this.$u.vuex('defcku', {})
						}
						this.cku.splice(index, 1)
						this.$u.vuex('cku', this.cku)
					}
					this.$nextTick(() => {
						this.list.splice(rowIndex, 1)
						this.$refs.uToast.show({ title: '删除成功', type: 'success', duration: '2300' })
					})
				}
			},
			handleNewSite() {
				this.$refs.refNewCku.handleShow()
			},
			handleSubmit({ data, index }) {
				if (index == -1) {
					this.list.splice(0, 0, data)
					if (this.site.index !== -1) {
						this.site.index++
					}
					return
				}
				// console.log(index)
				this.fetchData()
			}
		}
	}
</script>
<style lang="scss" scoped>
	.site {
		.impSite {
			display: flex;
		}

		.setting {
			height: 80rpx;
			line-height: 80rpx;

			.importBtn {
				margin: 0px 30rpx;
			}
		}

		.pushpin {
			margin-right: 30rpx;
		}
	}
</style>